﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Transactions;

public partial class MasterPage : System.Web.UI.MasterPage
{
    chodientuDataContext db = new chodientuDataContext();
    string Url = "http://www.vietcombank.com.vn/ExchangeRates/ExrateXML.aspx";
    protected void Page_Load(object sender, EventArgs e)
    {
        connection Conn = new connection();
        Conn.Open();
        seconds = (GetEndTime() - GetStartTime()).TotalSeconds;

        //check login 
        if (Session["user"] != null)
        {

            Login1.Visible = false;
            Login2.Visible = true;
            account.InnerText = Session["user"].ToString();
        }
        else
        {
            Login1.Visible = true;
            Login2.Visible = false;
        }
        drp_tinhthanh.DataSource = db.TINHTHANHs;

        //-----------------------
        if (Session["Cart"] != null)
        {
            List<string> giohang = (List<string>)Session["Cart"];
            soluong.Text = giohang.Count.ToString();
            //ms_soluong.Text = giohang.Count.ToString();

        }
        /*
        if (!IsPostBack)
        {

            DataSet ds = new DataSet();

            ds.ReadXml(Url);

            if (ds != null && ds.Tables.Count > 0)
            {

                DataTable dt = new DataTable();

                dt = ds.Tables["Exrate"];

                foreach (DataRow dr in dt.Rows)
                {
                    if (dr[0].ToString() == "USD")
                    {
                        Session["tigia"] = dr[4].ToString();
                        break;
                    }
                }
            }
        }*/
        if (!Page.IsPostBack)
        {
            
            this.fillRaovat(Conn);

            string sql1 = "select top 10 * from sanpham order by masanpham desc";
            DataTable sp = Database.GetData(sql1);
            DataList3.DataSource = sp;
            DataList3.DataBind();

        }

    }
    public void xem_giohang(object sender, EventArgs e)
    {
        Response.Redirect("giohang.aspx");
    }
    public double seconds;

    private DateTime GetStartTime()
    {
        return DateTime.Now;
    }
    private DateTime GetEndTime()
    {
        return new DateTime(2013, 5, 06, 8, 10, 0);
    }
    public void ImageButton1_Click(object sender, EventArgs e)
    {
        ImageButton image = (ImageButton)sender;
        String ProductID = image.CommandArgument;

        Response.Redirect("Muahang.aspx?sanpham=" + ProductID);
    }
    public lThanhVien getThanhVien(DataTable tv)
    {
        lThanhVien thanhvien = new lThanhVien();
        foreach (DataRow row in tv.Rows)
        {
            thanhvien.username = row["username"].ToString();
            thanhvien.soTaiKhoan = row["SoTaiKhoan"].ToString();
            thanhvien.diaChi = row["DiaChi"].ToString();
            thanhvien.diemTichLuy = float.Parse(row["diemTichLuy"].ToString());
            thanhvien.hoTen = row["hoten"].ToString();
            thanhvien.ngaySinh = DateTime.Parse(row["NgaySinh"].ToString());
        }
        return thanhvien;
    }
    public float getPrice(DataTable gia)
    {
        float g = 0;
        foreach (DataRow row in gia.Rows)
        {
            g = float.Parse(row["GiaTien"].ToString());
        }
        return g;
    }
    public int getCount(DataTable dem)
    {
        int d = 0;
        foreach (DataRow row in dem.Rows)
        {
            d = int.Parse(row["DEM"].ToString());
        }
        return d;
    }
    
    public void fillRaovat(connection Conn)
    {
        string sql = "SELECT top 10 * from raovat  order by maraovat desc";
        DataSet ds = new DataSet();
        DataTable raovat = new DataTable();
        raovat    = Conn.return_table(ds, sql, "RAOVAT");
        DataList2.DataSource = raovat;
        DataList2.DataBind();
    }
    public void SearchProduct(object sender, EventArgs e)
    {
        Response.Redirect("SearchProduct.aspx?keyword=" + search.Text);
    }

    //function for login / logout / reg
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        dangnhap(tbxTaiKhoan.Value.ToString(), tbxMatKhau.Value.ToString());
    }

    protected void dangnhap(string tk, string mk)
    {
        var query = db.ACCOUNTs.Where(n => n.USERNAME.Equals(tk) && n.PASSWORD.Equals(mk)).Select(n=>n.kindOfUser);
        if (query.Count() == 1)
        {
            Session["user"] = tbxTaiKhoan.Value.ToString();
            Session["type"] = query;
            Response.Redirect(Request.RawUrl);
        }
        else
        {
            tbxMatKhau.Value = "";
        }
    }

    protected void btnLogout_Click(object sender, EventArgs e)
    {
        Session.Abandon();
        Response.Redirect(Request.RawUrl);
    }

    protected void btn_dangky_Click(object sender, EventArgs e)
    {
        if (!tbx_matkhau.ToString().Equals(tbx_matkhau1.ToString()))
            return;

        Thanhvien tv = new Thanhvien();
        ACCOUNT acc = new ACCOUNT();
        tv.Hoten = tbx_hoten.Value.ToString();
        tv.Username = tbx_taikhoan.Value.ToString();
        tv.Diachi = tbx_diachi.Value.ToString();
        tv.Ngaysinh = DateTime.Parse(tbx_ngaysinh.Text);
        tv.Sotaikhoan = tbx_stk.Value.ToString();
        tv.Diemtichluy = 0;
        tv.dienthoai = tbx_dienthoai.Value.ToString();
        tv.email = tbx_email.Value.ToString();
        tv.tinhthanh = drp_tinhthanh.SelectedValue.ToString();

        acc.USERNAME = tbx_taikhoan.Value.ToString();
        acc.PASSWORD = tbx_matkhau.Value.ToString();
        acc.kindOfUser = 0;

        using (TransactionScope ts = new TransactionScope())
        {
            try
            {
                db.ACCOUNTs.InsertOnSubmit(acc);
                db.SubmitChanges();
                db.Thanhviens.InsertOnSubmit(tv);
                db.SubmitChanges();
                ts.Complete();
                Response.Write("<script>alert('Đăng ký thành công');</script>");
                return;
            }
            catch (Exception)
            {
                Response.Write("<script>alert('Đăng ký không thành công');</script>");
                return;
            }
        }
    }

}
